let player = require('./player');
let PoKerMgr = require('./PoKerController')

class Room{
    constructor(roomData,client) {
        console.log('创建房间的参数为：',roomData);
        this.roomID = roomData.room_id;
        this.createID = roomData.create_id;         //房主ID
        this.gameNumbers = roomData.game_numbers;   //游戏局数
        this.currentNumbers = 0; //当前游戏局数
        this.jieSanType = roomData.jie_san;             //解散类型  0--半数解散  1--全票解散
        console.log(`房间ID = ${this.roomID},解散房间类型 = ${this.jieSanType}`);

        this.seatIndexList = [0,1,2,3,4,5];
        this.playerList = [];
        this.cardsList = [];
        this.current_player_id = null,
        //当前房间下注总分数
        this.total_bet = 0,
        //上局胜利的玩家ID
        this.win_player_id = null
    }

    //把进入房间的玩家保存在本地房间信息内
     async addPlayer(userID, client) {
         console.log('将要加入的玩家的ID:', userID);
         let result = await global.PSZServerMgr.PSZDbMgr.getUserInfo(userID);
         if(result.length === 0){
            //没有在数据中查询到玩家信息
         }else{
             let tmpPlayer = new player(result[0],client);
             tmpPlayer.room = this;
             tmpPlayer.seatIndex = (this.seatIndexList.splice(0,1))[0];

             global.PSZServerMgr.PSZDbMgr.updataRoomInfo(this.roomID,this.playerList);
             //将玩家对象添加到room类中的玩家列表里面
             this.playerList.push(tmpPlayer);
             this.syncPlayerInfo();
         }
     }

    syncPlayerInfo(){
        let playerInfoList = [];
        for(let i = 0; i < this.playerList.length; i++){
            let playerInfo = this.playerList[i].getPlayerInfo();
            playerInfoList.push(playerInfo);
        }
        for(let i = 0 ; i < this.playerList.length; i++){
            let player = this.playerList[i];
            global.PSZServerMgr.PSZMessageMgr.sendMessage("sync_all_player_info",playerInfoList,player.client);
        }
    }

    //处理玩家点击准备按钮事件
    StartGameOrSyncReadyMessage(player){
        player.readyState = true;
        //如果房间内玩家全部准备了   那么可以不用同步准备消息  而是同步开始是游戏消息。
        if(this.isStartGame()){
            //如果开始游戏则同步游戏开始的消息
            this.syncStartGame();
        }else{
            //否则同步玩家准备的消息
            this.syncPlayerReadyOkMessage(player);
        }
    }

    syncPlayerReadyOkMessage(player){
        //下面这段代码只是同步了准备消息

        let playerInfoList = [];
        for(let i = 0; i < this.playerList.length; i++){
            let playerReadyState = this.playerList[i].getPlayerReadyState();
            playerInfoList.push(playerReadyState);
        }
        for(let i = 0 ; i < this.playerList.length; i++){
            let player = this.playerList[i];
            global.PSZServerMgr.PSZMessageMgr.sendMessage("sync_all_player_ready_state",playerInfoList,player.client);
        }

    }

    //每当有玩家发送准备消息的时候做出判断，判断是否该开始游戏
    //返回true 说明可以开始游戏  false则玩家未全部准备
    isStartGame(){
        let res = true
        this.playerList.forEach((player)=>{
            if(!player.readyState){
                res = false;
            }
        })
        return res;
    }

    //同步游戏开始的消息
    syncStartGame(){
        if(this.currentNumbers == 0)
        {
            this.current_player_id = this.playerList[0].userID;
            console.log(`当前玩家ID= ${this.playerList[0].userID}`);
        }
        else{
            this.current_player_id = this.win_player_id;
        }
        //当前房间下注总分数
        this.total_bet = 0;
        //1.服务端需要给每个玩家发牌.
        //获取一副新牌
        this.cardsList = global.PSZServerMgr.PoKerMgr.getNewPoKerCards();
        for(let i = 0; i < this.playerList.length; i ++)
        {
            let player = this.playerList[i];
            for (let j = 0; j < 3; j++) {
                player.cards.push(this.cardsList.pop());
            }
        }

        let player_InfoList = [];

        for (let i = 0; i < this.playerList.length; i++) {
            player_InfoList.push(this.playerList[i].getPlayerInfo());
        }

        let data = {
            playerInfoList:player_InfoList
        }
        //2.给每个玩家同步开始游戏的消息   客户端处理消息
        this.playerList.forEach((player)=>{
            global.PSZServerMgr.PSZMessageMgr.sendMessage("start_game", data,player.client);
        })
    }

    getRoomInfo(){
        return {
            room_id:this.roomID,
            create_id:this.createID,
            game_numbers:this.gameNumbers,
            current_numbers:this.currentNumbers,
        }
    }
}
module.exports = Room;